package com.acs.player;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.acs.player.bdd.Adapter;
import com.acs.player.constants.PlayerConst;
import com.acs.player.entities.Music;
import com.acs.player.entities.Playlist;
import com.acs.player.media.Player;
import com.acs.player.media.RetrieveData;
import com.acs.player.views.playlist.PlaylistAdapter;
import com.acs.player.views.playlist.PlaylistUtils;
import com.acs.player.views.simple.SimpleAdapter;
import com.acs.player.views.simple.TrackAdapter;

public class PlaylistActivity extends Activity
{
	private List<Playlist> playlists;
	private Menu menu;
	private Adapter adpt = new Adapter(this);
	private TrackAdapter track;
	private List<String> playlistNames;
	private List<Music> trackliste;

	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playlist_view); 
        Adapter adpt = new Adapter(this);
        //adpt.test();
        
    	RetrieveData retrieveData = RetrieveData.getRetrieveData(this);
        trackliste = retrieveData.getAllTracksList();
        //adpt.verifMusicSdCard(trackliste);
        
        loadPlayLists();
		
        //CREATE MENU
		RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.the_mother_of_all_views_navigator);       
        menu = new Menu();
        LinearLayout linearLayout = menu.createMenu(this);
        relativeLayout.addView(linearLayout);
    }
	
	@Override
	protected void onNewIntent(Intent intent) {
		//LauncheMode "simple top"
		super.onNewIntent(intent);
	}
	 
	 public void addPlaylist(View view){
		 	AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);  
	        alertDialog.setTitle(R.string.add_to_playlist);
	        
	        final EditText editText = new EditText(this);
	        alertDialog.setView(editText);
	         
	        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	        	  public void onClick(DialogInterface dialogInterface, int arg1) {
	        		  Editable editable = editText.getText();
	        		  String playListName = editable.toString();  		  
	        		  updatePlaylist(playListName);     		
	        	  }});
	        
	        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	       
	        	  public void onClick(DialogInterface dialogInterface, int arg1) {
	        		  dialogInterface.cancel();
	        	  }});
	        
	        alertDialog.show();
	 }
	 
	 public void DeletePlayliste(View view)
	 {
		 	
		 	AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);  
	        alertDialog.setTitle(R.string.delete_playliste);
	        
	        playlistNames = adpt.getPlaylistNames(); //Recupere le nom des playlists
	        
	        SimpleAdapter simpleAdapter = new SimpleAdapter(this, R.layout.row, playlistNames);
	        alertDialog.setAdapter(simpleAdapter, new DialogInterface.OnClickListener() 
	        {
	        	
				@Override
				public void onClick(DialogInterface dialog, int which) 
				{				
					String playlistName = playlistNames.get(which);
					alertdialogSupprPlayliste(playlistName);
					
				}
	        });
	        
	        alertDialog.show();
	 }
	 
	 public void alertdialogSupprPlayliste(final String playlistName)
	 {
		 Log.d("toto", playlistName);
		 AlertDialog.Builder alertDialoga = new AlertDialog.Builder(this);  
	        
	        final TextView textView = new TextView(this);
	        textView.setText("Etes vous sur de vouloir supprimer cette playliste ?");
	        alertDialoga.setView(textView);
	         
	        alertDialoga.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	        	  public void onClick(DialogInterface dialogInterface, int arg1) {
	        		  			
						adpt.deletePlaylist(playlistName);			
						 loadPlayLists();    		    		
	        	  }});
	        
	        alertDialoga.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	       
	        	  public void onClick(DialogInterface dialogInterface, int arg1) {
	        		  dialogInterface.cancel();
	        	  }});
	        
	        alertDialoga.show();
			
	 }
	 
	 public void loadPlayLists()
	 {
		//Recupere dans la base toutes les playlistes
		playlists = adpt.extractPlaylists();
		PlaylistAdapter playlistAdapter = new PlaylistAdapter(this, playlists);
		ExpandableListView expandableList = (ExpandableListView) findViewById(R.id.expandableView);
		expandableList.setAdapter(playlistAdapter); 
	 }
	 
	 public void updatePlaylist(String playListName)
	 {
		  Playlist newPlaylist = new Playlist(playListName);	  
		  //INSERER DANS LA BASE LA NOUVELLE PLAYLIST
		  adpt.addPlaylist(playListName);
		  
		  loadPlayLists();
	 }
	 
    public void click(View view){
    	menu.click(view);
    }
    
    public void goToPlayerActivity(int playlistPosition, int musicPosition){
    	
    	List<Music> playlistTracks = playlists.get(playlistPosition).getMusicList();
    	Player player = Player.getPlayer();
		if(!player.getTrackListsToPlay().equals(playlistTracks)){
			player.setTrackListsToPlay(playlistTracks);
			player.setCurrentActivity(PlayerConst.PLAYLIST_VIEW);
		}
    	
    	Intent intent = new Intent(PlaylistActivity.this, PlayerActivity.class); 
    	intent.putExtra(PlayerConst.CURRENT_TRACK_INDEX_KEY, musicPosition);
    	intent.putExtra(PlayerConst.PREVIOUS_ACTIVITY_KEY, PlayerConst.PLAYLIST_VIEW);
    	
    	startActivity(intent);
    }
    
}